/******************************************************************
*
* Copyright 2010 Darshan M Sonde
*
* This file is part of ddots.
*
* ddots is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ddots is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ddots.  If not, see <http://www.gnu.org/licenses/>.
*
*********************************************************************/

#include "engineinterface.h"
#include "level0.h"


/* This is a basic random player. it stores all valid moves
   in init. on requesting or performing move . it removes from the
   valid moves list one move.
   */
Level0Engine::Level0Engine()
    :mName("Level 0")
{
};

void Level0Engine::initGame(int rows,int cols)
{
    mValidMoves.clear();
    for(int i=0;i<rows;++i)
    {
        for(int j=0;j<cols;++j)
        {
            if( j != cols-1 )
            {
                mValidMoves.append(
                        QLine(QPoint(i,j),QPoint(i,j+1)) );
            }

            if( i != rows-1 )
            {
                mValidMoves.append(
                        QLine(QPoint(i,j),QPoint(i+1,j)) );
            }
        }
    }
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
}

bool Level0Engine::doMove(int startRow, int startCol, int endRow, int endCol)
{
    if(mValidMoves.isEmpty())
        return false;

    foreach(QLine line,mValidMoves) {
        if( (line.x1()==startRow)
            &&(line.y1()==startCol)
            &&(line.x2()==endRow)
            &&(line.y2()==endCol) ) {
            mValidMoves.removeOne(line);
            return true;
        }
    }
    return false;
}

bool Level0Engine::getMove(int &startRow, int &startCol, int &endRow, int &endCol)
{
    if(mValidMoves.size()==0)
        return false;

    int val=qrand()%mValidMoves.size();
    QLine line=mValidMoves.takeAt(val);
    startRow = line.x1();
    startCol = line.y1();
    endRow = line.x2();
    endCol = line.y2();
    return true;
}

Q_EXPORT_PLUGIN2(level0engine, Level0Engine);

